package com.cd.javacxy.common.model.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.cd.javacxy.common.model.enums.TopicType;
import com.cd.javacxy.common.utils.CategoryCache;
import com.cd.javacxy.common.utils.DateUtil;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * cxy_topic实体类
 *
 * @author yhy
 */
@Data
public class CxyTopic {
    /***/
    private Integer topicId;
    /**
     * 0是普通贴 1是投票贴
     */
    private TopicType topicType;
    /**
     * 组ID
     */
    private Integer pCategoryId;
    /**
     * 类型ID
     */
    @NotBlank(message = "请选择分类")
    private Integer categoryId;
    /**
     * 标题
     */
    @NotBlank(message = "请输入标题")
    @Length(message = "标题长度不能超过200个字符", max = 200)
    private String title;
    /**
     * 内容
     */
    @NotBlank(message = "请输入内容")
    @Length(min = 10, max = 2000, message = "内容长度10个~2000字符")
    private String content;
    /**
     * 内容摘要
     */
    private String summary;
    /**
     * 作者ID
     */
    private Integer userId;
    /**
     * 作者头像
     */
    private String userIcon;
    /**
     * 作者名字
     */
    private String userName;
    /**
     * 发表时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    /**
     * 最后评论时间
     */
    private LocalDateTime lastCommentTime;
    /**
     * 评论人数
     */
    private Integer commentCount;
    /**
     * 阅读人数
     */
    private Integer readCount;
    /**
     * 收藏人数
     */
    private Integer collectionCount;
    /**
     * 喜欢人数
     */
    private Integer likeCount;
    /**
     * 0是普通帖 1是置顶帖
     */
    private Integer grade;
    /**
     * 0是非精华 1是精华
     */
    private Integer essence;
    /***/
    private String topicImage;
    private String[] topicImageArray;
    /**
     * 文章缩列图
     */
    private String topicImageThum;

    private CxyAttachment attachment;
    private Integer topicCount;
    /**
     * //二级栏目名称
     */
    private String categoryName;
    /**
     * //一级栏目名称
     */
    private String pCategoryName;
    private Integer boolNew;

    public Integer getBoolNew() {
        if (DateUtil.daysBetween(createTime, new Date()) > 2) {
            return 0;
        }
        return 1;
    }

    public String getCategoryName() {
        return CategoryCache.getCategoryById(categoryId).getName();
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public String getpCategoryName() {
        return CategoryCache.getCategoryById(pCategoryId).getName();
    }

    public void setpCategoryName(String pCategoryName) {
        this.pCategoryName = pCategoryName;
    }
}
